package com.hopedove.workshop.ui

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hopedove.base.BaseActivity
import com.hopedove.data.Datetime
import com.hopedove.workshop.R
import com.hopedove.workshop.data.pojo.ObservableTakeInventoryItem
import com.hopedove.workshop.databinding.WorkshopTakeinventoryDetailActivityBinding
import com.hopedove.workshop.ui.adapter.TakeInventoryDetailAdapter
import com.hopedove.workshop.viewmodel.WorkshopTakeInventoryDetailViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import sh.tyy.wheelpicker.DatePicker
import sh.tyy.wheelpicker.DatePickerView
import java.util.*

class TakeInventoryDetailActivity : BaseActivity() {

    private lateinit var binding: WorkshopTakeinventoryDetailActivityBinding
    private val viewModel: WorkshopTakeInventoryDetailViewModel by viewModel()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =
            DataBindingUtil.setContentView(this, R.layout.workshop_takeinventory_detail_activity)
        with(binding) {
            lifecycleOwner = this@TakeInventoryDetailActivity
            vm = viewModel

            rvList.layoutManager =
                LinearLayoutManager(this@TakeInventoryDetailActivity, RecyclerView.VERTICAL, false)
        }

        setupViewModelObservers()
        viewModel.load()

        binding.etTriplenumber.setFocusable(true);
        binding.etTriplenumber.setFocusableInTouchMode(true);
        binding.etTriplenumber.requestFocus();
        binding.etTriplenumber.requestFocusFromTouch();
        binding.etTriplenumber.findFocus();
        binding.etTriplenumber.setOnKeyListener(View.OnKeyListener { v: View?, keyCode: Int, event: KeyEvent? ->
            if (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
                 viewModel.onMachineNoActionDone()
                 true
            }
            false
        })
    }

    private fun setupViewModelObservers() {
        observePickDate(viewModel.pickDate)
        observeToastText(viewModel.toastText)
        observeLoading(viewModel.loading)

        observeTakeInventoryList(viewModel.takeInventoryList)
    }

    private fun observePickDate(event: LiveData<Datetime>) {
        event.observe(this, { date ->
            showPicker { year, month, day ->
                viewModel.updateDate(Datetime(year, month + 1, 1))
            }

        })
    }
    private fun showPicker(minDate: Date? = null, maxDate: Date? = null, completion: (year: Int, month: Int, day: Int) -> Unit) {
        val picker = DatePicker(this)
        picker.show(window)
        picker.pickerView?.mode = DatePickerView.Mode.YEAR_MONTH
        picker.pickerView?.apply {
            this.minDate = minDate
            this.maxDate = minDate
            post {
                setDate(2022, 2, 0)
            }
        }
        picker.setOnClickOkButtonListener {
            val pickerView = picker.pickerView ?: return@setOnClickOkButtonListener
            completion(pickerView.year, pickerView.month, pickerView.day)
            picker.hide()
        }
    }

    private fun observeTakeInventoryList(data: LiveData<List<ObservableTakeInventoryItem>>) {
        data.observe(this, { list ->
            with(binding.rvList) {

                val batchAdapter = adapter as? TakeInventoryDetailAdapter
                batchAdapter?.updateDataSet(list)
                if (batchAdapter == null) {
                    adapter = TakeInventoryDetailAdapter().apply {
                        updateDataSet(list)
                    }
                }
            }
        })
    }


}